# -*- coding: utf-8 -*-

import os

from time import *

class MethodDescriptor(object):
    def __init__(self, func):
        self.func = func
        
    def __get__(self, instance, owner):
      
        pass
        """
        if instance is None:
            return new.instancemethod(self.func, owner, owner.__class__)
        else:
            return new.instancemethod(self.func, instance, owner)
        """
        
class PropertyDescriptor(object):
    def __init__(self, fget, fset, fdel):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
    def __get__(self, instance, owner):
        if instance is None:
            return self.fget(owner)
        else:
            return self.fget(instance)
    def __set__(self, instance, value):
        self.fset(instance, value)
    def __delete__(self, instance):
        self.fdel(instance)
        
def tm(func):
  print func
    #return "--- %s" %(func)

def hybrid_property(fget, fset=None, fdel=None):
    return PropertyDescriptor(fget, fset, fdel) 
  

class DataFile:
  
  def __init__(self, filename):
    self.filename = filename
    statinfo = os.stat(self.filename)
    (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, self.st_size, st_atime,  self.st_mtime, self.st_ctime) =  statinfo

  def getShortName(self):
    name = os.path.split(self.filename)
    return name[1]
    
  def getTime(self, type):
    if type == 'm':
      return strftime("%Y-%m-%d %H:%M:%S", gmtime(self.st_mtime))
    elif type == 'c':
      return strftime("%Y-%m-%d %H:%M:%S", gmtime(self.st_ctime))
    else:
      return None


class SDataFile:
  
  class __DataFile:
  
    def __init__(self, filename):
      self.filename = filename
      statinfo = os.stat(self.filename)
      (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, self.st_size, st_atime,  self.st_mtime, self.st_ctime) =  statinfo

    def getShortName(self):
      name = os.path.split(self.filename)
      return name[1]
      
    def getTime(self, type):
      if type == 'm':
        return strftime("%Y-%m-%d %H:%M:%S", gmtime(self.st_mtime))
      elif type == 'c':
        return strftime("%Y-%m-%d %H:%M:%S", gmtime(self.st_ctime))
      else:
        return None

  instance = None
          
  def __init__(self, filename):
    


    if not SDataFile.instance:
      SDataFile.instance = SDataFile.__DataFile(filename)
    else:
      pass

    
  #@tm
  def __getattr__(self, name):
    return getattr(self.instance, name)
    
  def __setattr__(self, name, value):
    return setattr(self.instance, name, value)

  

def test():
  print "begin test\n"
  file = SDataFile("F:/dev/excel/0.2/pipe/datafile.py")
  print file.getShortName()
  print file
  #file.reset("F:/dev/excel/0.2/pipe/main.py")
  #file.reset()
  print file.getShortName()
  print file.st_size
  #file.st_size = 10
  print file.st_size
  print file.getTime('m')
  print file.getTime('c')
  #print file.st_mtime
  file = DataFile("F:/dev/excel/0.2/pipe/main.py")
  print file
  print file.filename
  print file.st_size
  print "\nend test"

if __name__ == "__main__":
    test()          
